package cn.jinjimi.app.main.activity;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.base.util.ToastUtils;
import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.R;
import cn.jinjimi.app.controller.UserController;
import cn.jinjimi.app.controller.event.DataEvent;
import cn.jinjimi.app.controller.event.GroupEvent;
import cn.jinjimi.app.entity.Account;
import cn.jinjimi.app.entity.Group;
import cn.jinjimi.app.main.extend.ToolBarCommonActivity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by haner on 15-1-23.
 */
public class GroupActivity extends ToolBarCommonActivity implements AdapterView.OnItemClickListener {
    private ListView groupList;
    private Group group;

    @Override
    protected void beforeInitView() {
        setContentView(R.layout.activity_area);
        setTitle(R.string.info_company);
    }

    @Override
    protected void initial() {
        initView();
        initListener();
        initData();
    }

    protected void initView() {
        TextView chooseName = (TextView) findViewById(R.id.tv_choose_name);
        chooseName.setText(getString(R.string.company_had_chose));

        TextView companyChoosed = (TextView) findViewById(R.id.area_place_choosed);
        groupList = (ListView) findViewById(R.id.list_view);
    }

    protected void initListener() {
        groupList.setOnItemClickListener(GroupActivity.this);
    }

    protected void initData() {
        if (group == null) {
            getData();
        } else if (group.getCompanyTypes().size() > 0) {
            groupList.setAdapter(new MyAdapter());
        }
    }

    private void getData() {
        showLoadingIndicator();
        UserController.getCompanyType();

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGroupEvent(GroupEvent event) {
        if (isFinishing())
            return;

        switch (event.state) {
            case DataEvent.SUCC:
                group = event.data;
                if (group.getCompanyTypes().size() > 0) {
                    groupList.setAdapter(new MyAdapter());
                }
                break;
            case DataEvent.FAIL:
                ToastUtils.toastMessage(GroupActivity.this, getString(R.string.error_bad_network));
                break;
        }
        hideLoadingIndicator();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        getCompany(group.getCompanyTypes().get(position).getType(), group.getCompanyTypes().get(position).getName());
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return group.getCompanyTypes().size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(GroupActivity.this, R.layout.item_area_or_group, null);
            }
            TextView companyType = (TextView) convertView.findViewById(R.id.area_province);
            companyType.setText(group.getCompanyTypes().get(position).getName());
            return convertView;
        }

    }

    private void getCompany(String type, String companyName) {
        Intent intent = new Intent(this, CompanyActivity.class);
        intent.putExtra("type", type);
        intent.putExtra("companyName", companyName);
        startActivity(intent);
    }

}
